using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace ICP_MS.Plugin.LA300;

public class FormRatio : Form
{
	private ExperimentDatas experiment = null;

	private ExperimentDB db = null;

	private IContainer components = null;

	private Label label1;

	private Label label2;

	private ComboBox cbNumerator;

	private ComboBox cbDenominator;

	private Button btnAdd;

	private DataGridView dgvRatios;

	private GroupBox groupBox1;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;

	private DataGridViewTextBoxColumn colNumerator;

	private DataGridViewTextBoxColumn colDenominator;

	public FormRatio()
	{
		InitializeComponent();
	}

	private void FormRatio_Load(object sender, EventArgs e)
	{
		Text = Lang.Txt("FormRatio.Title.Text");
		groupBox1.Text = Lang.Txt("FormRatio.GroupBox.AddRatio.Text");
		label1.Text = Lang.Txt("FormRatio.Columns.Numerator.Text");
		label2.Text = Lang.Txt("FormRatio.Columns.Denominator.Text");
		btnAdd.Text = Lang.Txt("FormRatio.Button.Add.Text");
		colNumerator.HeaderText = Lang.Txt("FormRatio.Columns.Numerator.Text");
		colDenominator.HeaderText = Lang.Txt("FormRatio.Columns.Denominator.Text");
		base.WindowState = FormWindowState.Maximized;
		InitRatios();
		BindRatios();
	}

	public void SetParams(ExperimentDatas experiment, ExperimentDB db)
	{
		this.experiment = experiment;
		this.db = db;
	}

	private string[] InitRatios()
	{
		cbNumerator.Items.Clear();
		cbDenominator.Items.Clear();
		IEnumerable<string> symbols = experiment.SelectIsotopes.Select((AnalyteIsotope isotope) => isotope.symbol);
		if (symbols != null)
		{
			ComboBox.ObjectCollection items = cbNumerator.Items;
			object[] items2 = symbols.ToArray();
			items.AddRange(items2);
			ComboBox.ObjectCollection items3 = cbDenominator.Items;
			items2 = symbols.ToArray();
			items3.AddRange(items2);
			return symbols.ToArray();
		}
		return null;
	}

	private void BindRatios()
	{
		dgvRatios.AutoGenerateColumns = false;
		if (dgvRatios.DataSource == null)
		{
			dgvRatios.DataSource = experiment.Ratios;
			return;
		}
		BindingContext[dgvRatios.DataSource].SuspendBinding();
		dgvRatios.DataSource = null;
		dgvRatios.DataSource = experiment.Ratios;
		BindingContext[dgvRatios.DataSource].ResumeBinding();
		experiment.SetupRatioResults();
		experiment.Dirty.RatiosDirty = true;
	}

	public void UpdateSelectedIsotopes(object sender, EventArgs e)
	{
		string[] symbols = InitRatios();
		experiment.Ratios.RemoveAll((IsotopeRatio ratio) => !symbols.Contains(ratio.DenominatorSym) && !symbols.Contains(ratio.NumeratorSym));
		BindRatios();
	}

	private void btnAdd_Click(object sender, EventArgs e)
	{
		if (cbNumerator.SelectedItem == null || cbDenominator.SelectedItem == null)
		{
			MessageBox.Show(Lang.Txt("FormRatio.MessageBox.Info.SelectElement.Text"));
			return;
		}
		try
		{
			string symbol1 = cbNumerator.SelectedItem.ToString();
			string symbol2 = cbDenominator.SelectedItem.ToString();
			if (symbol1 == symbol2)
			{
				MessageBox.Show(Lang.Txt("FormRatio.MessageBox.Info.SelectDifferentElement.Text"));
				return;
			}
			AnalyteIsotope isotope2 = experiment.SelectIsotopes.Where((AnalyteIsotope isotope) => isotope.symbol == symbol1).First();
			AnalyteIsotope isotope3 = experiment.SelectIsotopes.Where((AnalyteIsotope isotope) => isotope.symbol == symbol2).First();
			if (experiment.Ratios.Exists((IsotopeRatio r) => r.NumeratorId == isotope2.id && r.NumeratorSym == isotope2.symbol && r.DenominatorId == isotope3.id && r.DenominatorSym == isotope3.symbol))
			{
				MessageBox.Show(Lang.Txt("FormRatio.MessageBox.Info.ExitedRatio.Text"));
				return;
			}
			IsotopeRatio ratio = new IsotopeRatio
			{
				NumeratorId = isotope2.id,
				NumeratorSym = isotope2.symbol,
				DenominatorId = isotope3.id,
				DenominatorSym = isotope3.symbol,
				Name = isotope2.symbol + "/" + isotope3.symbol
			};
			if (db.SaveExpIsotopeRatio(ratio))
			{
				experiment.Ratios.Add(ratio);
			}
			BindRatios();
		}
		catch
		{
			MessageBox.Show(Lang.Txt("FormRatio.MessageBox.Info.UnexistingRatio.Text"));
		}
	}

	private void dgvRatios_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Delete && dgvRatios.SelectedRows.Count > 0)
		{
			DeleteSelectedRatios();
		}
	}

	private void dgvRatios_MouseDown(object sender, MouseEventArgs e)
	{
		if (dgvRatios.SelectedRows.Count > 0 && e.Button == MouseButtons.Right)
		{
			ContextMenu menu = new ContextMenu();
			menu.MenuItems.Add(Lang.Txt("FormRatio.Delete.Text"), delegate
			{
				DeleteSelectedRatios();
			});
			menu.Show(dgvRatios, e.Location);
		}
	}

	private void DeleteSelectedRatios()
	{
		List<IsotopeRatio> ratios = new List<IsotopeRatio>();
		foreach (DataGridViewRow row in dgvRatios.SelectedRows)
		{
			ratios.Add(experiment.Ratios[row.Index]);
		}
		foreach (IsotopeRatio ratio in ratios)
		{
			if (db.DeleteExpIsotopeRatio(ratio))
			{
				experiment.Ratios.Remove(ratio);
			}
		}
		BindRatios();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
		this.label1 = new System.Windows.Forms.Label();
		this.label2 = new System.Windows.Forms.Label();
		this.cbNumerator = new System.Windows.Forms.ComboBox();
		this.cbDenominator = new System.Windows.Forms.ComboBox();
		this.btnAdd = new System.Windows.Forms.Button();
		this.dgvRatios = new System.Windows.Forms.DataGridView();
		this.colNumerator = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colDenominator = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		((System.ComponentModel.ISupportInitialize)this.dgvRatios).BeginInit();
		this.groupBox1.SuspendLayout();
		base.SuspendLayout();
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(59, 33);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(35, 12);
		this.label1.TabIndex = 0;
		this.label1.Text = "分子:";
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(59, 72);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(35, 12);
		this.label2.TabIndex = 1;
		this.label2.Text = "分母:";
		this.cbNumerator.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cbNumerator.FormattingEnabled = true;
		this.cbNumerator.Location = new System.Drawing.Point(112, 30);
		this.cbNumerator.Name = "cbNumerator";
		this.cbNumerator.Size = new System.Drawing.Size(106, 20);
		this.cbNumerator.TabIndex = 2;
		this.cbDenominator.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.cbDenominator.FormattingEnabled = true;
		this.cbDenominator.Location = new System.Drawing.Point(112, 69);
		this.cbDenominator.Name = "cbDenominator";
		this.cbDenominator.Size = new System.Drawing.Size(106, 20);
		this.cbDenominator.TabIndex = 3;
		this.btnAdd.Location = new System.Drawing.Point(250, 49);
		this.btnAdd.Name = "btnAdd";
		this.btnAdd.Size = new System.Drawing.Size(75, 23);
		this.btnAdd.TabIndex = 4;
		this.btnAdd.Text = "添加";
		this.btnAdd.UseVisualStyleBackColor = true;
		this.btnAdd.Click += new System.EventHandler(btnAdd_Click);
		this.dgvRatios.AllowUserToAddRows = false;
		this.dgvRatios.AllowUserToDeleteRows = false;
		dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle1.Font = new System.Drawing.Font("宋体", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.dgvRatios.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
		this.dgvRatios.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.dgvRatios.Columns.AddRange(this.colNumerator, this.colDenominator);
		dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window;
		dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText;
		dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
		this.dgvRatios.DefaultCellStyle = dataGridViewCellStyle2;
		this.dgvRatios.Location = new System.Drawing.Point(8, 152);
		this.dgvRatios.Name = "dgvRatios";
		this.dgvRatios.RowTemplate.Height = 23;
		this.dgvRatios.Size = new System.Drawing.Size(561, 274);
		this.dgvRatios.TabIndex = 5;
		this.dgvRatios.KeyDown += new System.Windows.Forms.KeyEventHandler(dgvRatios_KeyDown);
		this.dgvRatios.MouseDown += new System.Windows.Forms.MouseEventHandler(dgvRatios_MouseDown);
		this.colNumerator.DataPropertyName = "NumeratorSym";
		this.colNumerator.HeaderText = "分子";
		this.colNumerator.Name = "colNumerator";
		this.colNumerator.ReadOnly = true;
		this.colNumerator.Width = 200;
		this.colDenominator.DataPropertyName = "DenominatorSym";
		this.colDenominator.HeaderText = "分母";
		this.colDenominator.Name = "colDenominator";
		this.colDenominator.ReadOnly = true;
		this.colDenominator.Width = 200;
		this.groupBox1.Controls.Add(this.btnAdd);
		this.groupBox1.Controls.Add(this.cbDenominator);
		this.groupBox1.Controls.Add(this.cbNumerator);
		this.groupBox1.Controls.Add(this.label2);
		this.groupBox1.Controls.Add(this.label1);
		this.groupBox1.Location = new System.Drawing.Point(8, 8);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Size = new System.Drawing.Size(561, 122);
		this.groupBox1.TabIndex = 6;
		this.groupBox1.TabStop = false;
		this.groupBox1.Text = "添加同位素比率";
		this.dataGridViewTextBoxColumn1.HeaderText = "分子";
		this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
		this.dataGridViewTextBoxColumn1.Width = 200;
		this.dataGridViewTextBoxColumn2.HeaderText = "分母";
		this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
		this.dataGridViewTextBoxColumn2.Width = 200;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(657, 485);
		base.Controls.Add(this.groupBox1);
		base.Controls.Add(this.dgvRatios);
		base.Name = "FormRatio";
		base.ShowIcon = false;
		this.Text = "同位素比率";
		base.Load += new System.EventHandler(FormRatio_Load);
		((System.ComponentModel.ISupportInitialize)this.dgvRatios).EndInit();
		this.groupBox1.ResumeLayout(false);
		this.groupBox1.PerformLayout();
		base.ResumeLayout(false);
	}
}
